package com.hu.network.util;

import java.io.File;
import java.io.IOException;

/**
 * 说明：
 *
 * @author ： hucanhua
 * @date ： 2017/10/27
 */
public class FileUtils {

    public static File newFile(String pathAndName) throws IOException {
        return newFile(new File(pathAndName));
    }

    public static File newFile(String path, String name) throws IOException {
        return newFile(new File(path, name));
    }

    public static File newFile(File parentFile, String name) throws IOException {
        return newFile(new File(parentFile, name));
    }

    public static File newFile(File file) throws IOException {
        if (newDir(file.getParentFile())) {
            if (file.exists() && file.isFile()) {
                return file;
            } else {
                return file.createNewFile() ? file : null;
            }
        }
        return null;
    }

    public static boolean newDir(String path) {
        return newDir(new File(path));
    }

    private static boolean newDir(File file) {
        return file != null && (!(!file.exists() || !file.isDirectory()) || file.mkdirs());
    }
}
